package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAdminExample;
import com.cskaoyan.market.db.domain.MarketOrder;
import com.cskaoyan.market.db.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.MybatisUtils;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.MarketOrderGoodsVo;
import com.cskaoyan.market.vo.MarketOrderVo;
import com.cskaoyan.market.vo.MarketStatUsersVo;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.invoke.VarHandle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@WebServlet("/admin/stat/*")
public class AdminStatController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/stat/", "");
        if("goods".equals(op))
        {
            goods(req,resp);
        }
        else if ("order".equals(op))
        {
            order(req,resp);
        }
        else if("user".equals(op))
        {
            statUser(req,resp);
        }
    }

    private void statUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        SqlSession session = MybatisUtils.getSession();
        MarketUserMapper userMapper = session.getMapper(MarketUserMapper.class);

        ArrayList<MarketStatUsersVo> userList = new ArrayList<>();
        List<MarketStatUsersVo> marketUsers = userMapper.countUserDataByDate();
        userList.addAll(marketUsers);

        HashMap<String, Object> data = new HashMap<>();
        data.put("columns", new String[]{"day", "users"});
        data.put("rows", userList);

        Object ok = ResponseUtil.ok(data);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().print(s);


    }

    private void order(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        SqlSession session = MybatisUtils.getSession();
        MarketOrderMapper orderMapper = session.getMapper(MarketOrderMapper.class);

        ArrayList<MarketOrderVo> orderVolist = new ArrayList<>();
        List<MarketOrderVo> marketOrderVos = orderMapper.countOrderDataByDate();
        orderVolist.addAll(marketOrderVos);

        HashMap<String, Object> data = new HashMap<>();
        data.put("columns",new String[]{"day","orders","customers","amount","pcr"});
        data.put("rows",orderVolist);

        Object ok = ResponseUtil.ok(data);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().write(s);


    }

    private void goods(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        SqlSession session = MybatisUtils.getSession();
        MarketOrderGoodsMapper orderGoodsMapper = session.getMapper(MarketOrderGoodsMapper.class);

        ArrayList<MarketOrderGoodsVo> orderGoodsList = new ArrayList<>();
        List<MarketOrderGoodsVo> orderGoodsVos = orderGoodsMapper.countOrderGoodsDataByDate();
        orderGoodsList.addAll(orderGoodsVos);

        HashMap<String, Object> data = new HashMap<>();
        data.put("columns",new String[]{"day","orders","products","amount"});
        data.put("rows",orderGoodsList);

        Object ok = ResponseUtil.ok(data);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().println(s);

    }




    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}
